package com.acme;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class PlayerSearch extends ListActivity {

	ArrayList<Player> results;
	TextView searchText;
	String searchTerm = null;

	private PlayerSearchResponseReceiver receiver;
	
	public class PlayerSearchResponseReceiver extends BroadcastReceiver {
		   public static final String ACTION_RESP = "com.test.intent.action.PLAYER_SEARCH_RECV";
		 
		   @Override
		    public void onReceive(Context context, Intent intent) {
		       	String response = intent.getStringExtra(FootballStatsService.SEARCH_OUT_JSON);
		       	results = Player.getPlayers(response);
				setListAdapter(
					new PlayerListAdapter(
						PlayerSearch.this.getApplicationContext(),
						R.layout.player_list_item, 
						results.toArray(new Player[results.size()])
					)
				);
		    }
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_name);
		results = new ArrayList<Player>();

		Button searchBtn = (Button) findViewById(R.id.searchBtn);
		searchText = (TextView) findViewById(R.id.searchText);

		if (savedInstanceState != null
				&& savedInstanceState.containsKey("searchTerm")) {
			this.searchTerm = savedInstanceState.getString("searchTerm");
			search(searchTerm);
		}

		searchBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// hide soft keyboard
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);

				// for now:
				searchTerm = searchText.getText().toString();
				search(searchTerm);
			}

		});

	}

	public void onListItemClick(ListView parent, View view, int position,long id) {
		Player selected = (Player) this.getListAdapter().getItem(position);
		Intent newIntent = new Intent(this, PlayerProfile.class);
		newIntent.putExtra(Player.PLAYER_ID, selected.id);
		startActivity(newIntent);
	}

	// save instance state for rotations
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		// Save UI state changes to the savedInstanceState.
		// This bundle will be passed to onCreate if the process is
		// killed and restarted.
		savedInstanceState.putString("searchTerm", searchTerm);
		super.onSaveInstanceState(savedInstanceState);
	}

	public void search(String term) {
		
		IntentFilter filter = new IntentFilter(PlayerSearchResponseReceiver.ACTION_RESP);
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		receiver = new PlayerSearchResponseReceiver();
		registerReceiver(receiver, filter);

		Intent msgIntent = new Intent(this, FootballStatsService.class);
		msgIntent.putExtra(FootballStatsService.ACTION,FootballStatsService.ACTION_SEARCH);
		msgIntent.putExtra(FootballStatsService.SEARCH_IN_QUERY, term);
		startService(msgIntent);
	}

	public void onStart() {
		super.onStart();
		// Bind to LocalService
	}

	public void onStop() {
		super.onStop();
	}
}
